package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.MemberThreadLocal;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.PageQueryDto;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.CountByTimeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: JBY
 * @since: 2025/08/22 14:28
 */
@Service
@Slf4j
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService {

    @Autowired
    private MemberReservationMapper memberReservationMapper;

    /**
     * 查询当天取消预约数量
     * @param
     * @return
     */
    @Override
    public Long getCancelledCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        //获取线程中的用户
        Long memberId = MemberThreadLocal.getMemberId();
        Long count = this.lambdaQuery().eq(Reservation::getUpdateBy, memberId)
                .between(Reservation::getUpdateTime, startTime, endTime)
                .count();
        return count;
    }

    /**
     * 查询每个时间段剩余预约次数
     * @param
     * @return
     */
    @Override
    public List<CountByTimeVo> getCountByTime(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(30);
        //select time, 6 - COUNT(*) as count from reservation where `time` between '2022-03-01' and '2022-03-31' and status != 2 group by `time`;
        List<CountByTimeVo> list = memberReservationMapper.selectCountByTime(startTime, endTime);
        return list;
    }

    /**
     * 新增预约
     * @param reservationDto
     * @return AjaxResult
     */
    @Override
    public String add(ReservationDto reservationDto) {
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(reservationDto, reservation);
        if(this.save(reservation)){
            return "操作成功";
        }
        return "网络繁忙, 请稍后再试";
    }

    /**
     * 分页查询预约
     * @param pageQueryDto
     * @return TableDataInfo
     */
    @Override
    public TableDataInfo<Reservation> pageQuery(PageQueryDto pageQueryDto) {
        Page<Reservation> page = new Page<>(pageQueryDto.getPageNum(), pageQueryDto.getPageSize());
        this.lambdaQuery().eq(Reservation::getStatus, pageQueryDto.getStatus())
                .page(page);
        TableDataInfo<Reservation> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    /**
     * 取消预约
     * @param id
     * @return AjaxResult
     */
    @Override
    public boolean cancel(Long id) {
        Reservation reservation = this.getById(id);
        if (ObjUtil.isNotNull(reservation) && reservation.getStatus().equals(0)) {
            reservation.setStatus(2);
            return this.updateById(reservation);
        }
        return false;
    }
}
